package com.snowRemovingUser.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.snowRemovingUser.domain.MDevice;
import com.snowRemovingUser.domain.MDeviceAttribute;
import com.snowRemovingUser.mapper.MDeviceAttributeMapper;
import com.snowRemovingUser.service.IMDeviceAttributeService;
import com.snowRemovingUser.service.IMDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备实况Controller
 *
 * @author lishiguang
 * @date 2023-05-06
 */
@RestController
@RequestMapping("/snowRemoving/device")
public class MDeviceController extends BaseController
{
    @Autowired
    private IMDeviceService mDeviceService;

    @Autowired
    private MDeviceAttributeMapper deviceAttributeMapper;

    @Autowired
    private IMDeviceAttributeService mDeviceAttributeService;

    /**
     * 查询设备实况列表
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(MDevice mDevice)
    {
        startPage();
        List<MDevice> list = mDeviceService.selectMDeviceList(mDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备实况列表
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:device:export')")
    @Log(title = "设备实况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MDevice mDevice)
    {
        List<MDevice> list = mDeviceService.selectMDeviceList(mDevice);
        ExcelUtil<MDevice> util = new ExcelUtil<MDevice>(MDevice.class);
        util.exportExcel(response, list, "设备实况数据");
    }

    /**
     * 获取设备实况详细信息
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:device:query')")
    @GetMapping(value = "/{serialNumber}")
    public AjaxResult getInfo(@PathVariable("serialNumber") String serialNumber)
    {
        return success(mDeviceService.selectMDeviceBySerialNumber(serialNumber));
    }

    /**
     * 新增设备实况
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:device:add')")
    @Log(title = "设备实况", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MDevice mDevice)
    {
        return toAjax(mDeviceService.insertMDevice(mDevice));
    }

    /**
     * 修改设备实况
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:device:edit')")
    @Log(title = "设备实况", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MDevice mDevice)
    {
        return toAjax(mDeviceService.updateMDevice(mDevice));
    }

    /**
     * 删除设备实况
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:device:remove')")
    @Log(title = "设备实况", businessType = BusinessType.DELETE)
	@DeleteMapping("/{serialNumbers}")
    public AjaxResult remove(@PathVariable String[] serialNumbers)
    {
        return toAjax(mDeviceService.deleteMDeviceBySerialNumbers(serialNumbers));
    }


    /**
     * 获取部门树列表
     */
    @PreAuthorize("@ss.hasPermi('snowRemoving:device:list')")
    @GetMapping("/deiceTreeSelect")
    public AjaxResult deviceTree(MDevice mDevice)
    {
        return success(mDeviceService.selectDeviceTreeList(mDevice));
    }




    /**
     * 查询设备各个属性
     */
    @GetMapping("/attributeList")
    public TableDataInfo deviceAttributeList(MDeviceAttribute mDeviceAttribute) {

        startPage();
        List<MDeviceAttribute> list = mDeviceService.selectDeviceAttributeList(mDeviceAttribute);
        return getDataTable(list);
    }



    /**
     * 启用设备
     */
    @PostMapping("/attributeEnable")
    public AjaxResult handleAttributeEnable(MDeviceAttribute deviceAttribute){

        return mDeviceAttributeService.handleAttributeEnable(deviceAttribute);
    }



    /**
     * 禁用设备
     */
    @PostMapping("/attributeDisable")
    public AjaxResult handleAttributeDisable(MDeviceAttribute deviceAttribute){


        return mDeviceAttributeService.handleAttributeDisable(deviceAttribute);
    }






}
